home *** CD-ROM | disk | FTP | other *** search
- Path: news.nstn.ca!news
- From: nstn2264@fox.nstn.ca (Robin Murray)
- Newsgroups: comp.lang.c++
- Subject: strstream and string::read_to_delim()
- Date: Sat, 03 Feb 1996 04:17:43 GMT
- Organization: Nova Scotia Technology Network
- Message-ID: <4euk0k$pkg@news.nstn.ca>
- NNTP-Posting-Host: ts1-05.mon.inforamp.net
- X-Newsreader: Forte Free Agent v0.46
-
- i've spent the last two days carving out a piece of problem code and
- narrowing it down to the following short program. what i want to
- do is extract newline-terminated strings (\r\n) into string classes
- using ansi string::read_to_delim(strstream). once i read the last
- string in, i want to then reset the strstream to put the get/put
- pointers back to the beginning of the stream, and place a null
- terminator at the beginning. further calls to read_to_delim will
- then (hopefully) return a null string.
-
- using bc++ 4.02, this doesn't always happen. when the string reaches
- 64 chars in length or more, i get wierd results. the first
- read_to_delim works fine. i set the get/put pointers to 0, and insert
- a null terminator. then the second call to read_to_delim returns
- chars from 64+ to the end of the string, instead of a null string.
-
- can anyone tell me why? thanks...
-
- #include <iostream.h>
- #include <strstrea.h>
- #include <cstring.h>
-
- int main()
- {
- strstream streamTest; string stringTest1, stringTest2;
-
- // the following works...
- // streamTest << "ABCDEFGH\r\n" << ends;
- // the following fails...
- streamTest <<
- "testingtestingtestingabcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n"
- << ends;
- // the following fails...
- // streamTest <<
- "testingtestingtestingabcdefghijklmnopqrstuvwxyz1234567890ABCDEF\r\n"
- << ends;
- // the following works...only one shorter that the one above
- // streamTest <<
- "testingtestingtestingabcdefghijklmnopqrstuvwxyz1234567890ABCDE\r\n"
- << ends;
- // the following works...when there is no \r\n
- // streamTest <<
- "testingtestingtestingabcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- << ends;
-
- // stringTest1 get up to the "\n", leaves "\n" in the stream
- stringTest1.read_to_delim( streamTest );
- // reset stream to the beginning, and put eof at beginning of stream
- streamTest.rdbuf()->seekpos( 0 );
- streamTest << ends;
- // this SHOULD produce a null string. doesn't all the time...
- stringTest2.read_to_delim( streamTest );
- return 0; // set a break point here, and inspect "stringTest2"
- }
-
- --
- Robin Murray
- Moncton, New Brunswick
- 506-853-3779
-
-